package com.oceansoft.core.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 对象处理
 * 
 * @author Administrator
 * 
 */
public final class ObjectUtil {
	public static final Object[] NULL_OBJECTS = new Object[0];

	public static Object copyValue(Object r_Value) {
		if (null == r_Value) {
			return r_Value;
		}

		Class<? extends Object> t_Type = r_Value.getClass();

		if (t_Type.isArray()) {
			int t_Length = Array.getLength(r_Value);
			Object[] t_Objects = (Object[]) (Object[]) Array.newInstance(t_Type.getComponentType(), t_Length);
			for (int i = 0; i < t_Length; ++i) {
				Object t_Object = Array.get(r_Value, i);
				t_Objects[i] = copyValue(t_Object);
			}

			return t_Objects;
		}

		return copySingleValue(r_Value);
	}

	private static Object copySingleValue(Object value) {
		try {
			Method method = value.getClass().getMethod("clone", (Class[]) null);
			if ((null != method) && ((method.getModifiers() & 0x1) != 0)) {
				return method.invoke(value, ArrayUtil.NULL_OBJECTS);
			}

			return value;
		} catch (Exception e) {
		}
		return value;
	}

	public static String getIdentityHexString(Object obj) {
		return Integer.toHexString(System.identityHashCode(obj));
	}

	public static Object bytes2Object(byte[] bytes, ClassLoader loader) {
		ByteArrayInputStream instrem = new ByteArrayInputStream(bytes);
		SpecifyLoaderObjectInputStream oin = null;
		try {
			oin = new SpecifyLoaderObjectInputStream(instrem, loader);
			Object localObject1 = oin.readObject();

			return localObject1;
		} catch (Throwable error) {
			error.printStackTrace();
		} finally {
			try {
				oin.close();
			} catch (Throwable ignored) {
			}
			try {
				instrem.close();
			} catch (IOException ignored) {
			}
		}
		return null;
	}

	public static byte[] object2Bytes(Object object) {
		ByteArrayOutputStream byteOut = null;
		ObjectOutputStream objectOut = null;
		try {
			byteOut = new ByteArrayOutputStream();
			objectOut = new ObjectOutputStream(byteOut);
			objectOut.writeObject(object);
			byte[] arrayOfByte = byteOut.toByteArray();

			return arrayOfByte;
		} catch (Throwable error) {
			error.printStackTrace();
		} finally {
			try {
				byteOut.close();
			} catch (IOException ignored) {
			}
			try {
				objectOut.close();
			} catch (IOException ignored) {
			}
		}
		return null;
	}

	static class SpecifyLoaderObjectInputStream extends ObjectInputStream {
		private ClassLoader loader;

		public SpecifyLoaderObjectInputStream(InputStream in, ClassLoader loader) throws IOException {
			super(in);
			this.loader = loader;
		}

		protected Class<?> resolveClass(ObjectStreamClass desc) throws ClassNotFoundException, IOException {
			try {
				return super.resolveClass(desc);
			} catch (ClassNotFoundException ce) {
			}
			return Class.forName(desc.getName(), false, this.loader);
		}
	}
	
	public static Map<String,Object> Object2Map(Object obj)
	{
		Map<String,Object> map = new HashMap<String, Object>();
		
		if(obj instanceof Map)
		{
			return (Map<String,Object>)obj;
		}
		
		
		try {
			Method[] methods = obj.getClass().getDeclaredMethods(); // 获取该类中定义的所有方法
			for (Method m : methods) { // 遍历这个方法数组
				if ( m.getName().startsWith("get")) { // 找出里面所有的set方法，准备赋值

					String fileName = m.getName().substring(3); // 获取map的key值，即request过来的参数名称
					//取出数据
					Object value = m.invoke(obj);// 			
					if(null!=value)
					{
						map.put(fileName.substring(0,1).toLowerCase()+fileName.substring(1),value);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return map;
	}
}
